/*
 * GroupsInterface.java
 *
 * Created on May 11, 2006, 11:25 AM
 *
 * @author  Francois Daniels
 */

import java.awt.*;
import java.awt.event.*;
import za.gateway.preferences.Settings;
import za.gateway.io.*;
import za.gateway.backend.*;
import za.gateway.container.*;

public class GroupsInterface extends javax.swing.JFrame 
        implements WindowListener {
    ConfigurationInterface parent;
    boolean databaseChanged;
    
    /** Creates new form GroupsInterface */
    public GroupsInterface () {
        initComponents ();
        parent = null;
        updateComponents ();
    }
    
    private void updateComponents () {
        databaseChanged = false;
        this.addWindowListener (this);
        setTitle ("Groups");
        readGroups ();
        setDefaultCloseOperation 
                (javax.swing.WindowConstants.HIDE_ON_CLOSE);
        centerOnParentAndShow ();
        if (Settings.isWindows) {
            try {
                javax.swing.UIManager.setLookAndFeel (
                    "com.sun.java.swing.plaf.windows." +
                        "WindowsLookAndFeel");
                javax.swing.SwingUtilities.
                        updateComponentTreeUI (this);
                pack ();
            } catch (Exception e) { }
        }
    }
    
    public void setVisible (boolean b) {
        readGroups ();
        centerOnParentAndShow ();
        super.setVisible (b);
    }
    
    private void centerOnParentAndShow () {
        pack ();
        if (parent == null) return;
        
        Dimension dparent = parent.getSize ();
        Dimension window = getSize ();
        int xCoord, yCoord;
        try {
            xCoord = parent.getLocationOnScreen ().x + 
                    (dparent.width/2 - window.width/2);
            yCoord = parent.getLocationOnScreen ().y + 
                    (dparent.height/2 - window.height/2);
        } catch (IllegalComponentStateException e) {return;}
        
        //Ensure that no part of the window will be off-screen
        Dimension screen = 
                Toolkit.getDefaultToolkit ().getScreenSize ();
        
        int xOffScreenExcess = xCoord + window.width - screen.width;
        if ( xOffScreenExcess > 0 ) {
            xCoord = xCoord - xOffScreenExcess;
        }
        if (xCoord < 0 ) {
            xCoord = 0;
        }
        int yOffScreenExcess = yCoord + window.height - screen.height;
        if ( yOffScreenExcess > 0 ) {
            yCoord = yCoord - yOffScreenExcess;
        }
        if (yCoord < 0) {
            yCoord = 0;
        }
        
        setLocation ( xCoord, yCoord );
    }
    
    /**
     * Reads the data from the database
     * It then outputs the data to the table
     **/
    private void readGroups () {
        Groups GroupsContainer = new Groups (Settings.userName,
                Settings.password, Settings.IP, Settings.database);
        Group [] GroupElements = GroupsContainer.GetData ();
        
        for (int i = 0; i < GroupElements.length; i++) {
            table.getModel ().setValueAt (GroupElements [i].GetName (),
                    i, 0);
            table.getModel ().setValueAt (
                    GroupElements [i].GetDescription (), i, 1);
        }
    }
    
    public GroupsInterface (ConfigurationInterface parent) {
        initComponents ();
        this.parent = parent;
        updateComponents ();
    }
    
    /**
     * This function is used to check when the window is closing.
     * Upon closing it will tell the parent wether or not to update
     * its data display.
     */
    public void windowClosing (WindowEvent e) {
        if (parent != null)
            parent.update (databaseChanged);
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Window Listener Defines ">
    public void windowClosed (WindowEvent e) {}
    public void windowDeactivated (WindowEvent e) {}
    public void windowIconified (WindowEvent e) {}
    public void windowDeiconified (WindowEvent e) {}
    public void windowActivated (WindowEvent e) {}
    public void windowDeactvated (WindowEvent e) {}
    public void windowOpened (WindowEvent e) {}
    // </editor-fold>
    
    
    // <editor-fold defaultstate="collapsed" desc=" Code generated by Netbeans for the GUI Components  ">
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        javax.swing.JMenu helpMenu;
        javax.swing.JSeparator jSeparator1;
        javax.swing.JMenu opsMenu;

        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        opsMenu = new javax.swing.JMenu();
        addItem = new javax.swing.JMenuItem();
        editItem = new javax.swing.JMenuItem();
        removeItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        exitItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        helpItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Groups");
        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"", ""},
                {"", ""},
                {"", ""},
                {"", null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Name", "Description"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(table);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        opsMenu.setText("Operations");
        addItem.setText("Add");
        addItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addItemActionPerformed(evt);
            }
        });

        opsMenu.add(addItem);

        editItem.setText("Edit");
        editItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editItemActionPerformed(evt);
            }
        });

        opsMenu.add(editItem);

        removeItem.setText("Remove");
        opsMenu.add(removeItem);

        opsMenu.add(jSeparator1);

        exitItem.setText("Exit");
        exitItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitItemActionPerformed(evt);
            }
        });

        opsMenu.add(exitItem);

        jMenuBar1.add(opsMenu);

        helpMenu.setText("Help");
        helpItem.setText("Help Contents");
        helpMenu.add(helpItem);

        jMenuBar1.add(helpMenu);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void exitItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitItemActionPerformed
        setVisible (false);
    }//GEN-LAST:event_exitItemActionPerformed
    
    private void editItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editItemActionPerformed
        String name = "";
        String password = "";
        String description = "";
        
        //TODO: Extract the valid fields from the database/table
        new NewGroupInterface (this, true).showWindow (name, password, 
                description);
    }//GEN-LAST:event_editItemActionPerformed
    
    private void addItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addItemActionPerformed
        new NewGroupInterface (this, true).setVisible (true);
    }//GEN-LAST:event_addItemActionPerformed
    
    public static void main (String args[]) {
        java.awt.EventQueue.invokeLater (new Runnable () {
            public void run () {
                new GroupsInterface ().setVisible (true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem addItem;
    private javax.swing.JMenuItem editItem;
    private javax.swing.JMenuItem exitItem;
    private javax.swing.JMenuItem helpItem;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenuItem removeItem;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables
    
}
